//
//  ShortcutService.swift
//  APP
//
//  Created by LXF on 2018/1/25.
//  Copyright © 2018年 Rdxer. All rights reserved.
//

import Foundation
import XXLogger

public enum AppShortcutItem:String {
    case message
    case vacation
    case salary
    case approval
}

extension AppShortcutItem{
    static func makeItem(string:String)->AppShortcutItem?{
        switch  string{
        case message.rawValue:
            return AppShortcutItem.message
        case vacation.rawValue:
            return AppShortcutItem.vacation
        case salary.rawValue:
            return AppShortcutItem.salary
        case approval.rawValue:
            return AppShortcutItem.approval
        default:
            return nil
        }
    }
}

/// 缺个 --> 优先级
public class ShortcutService {
    
    public static let shared = ShortcutService.init()
    
    /// 未处理 的 节点 集合
    public var unProcessedShortcutAction:[AppShortcutItem:Any] = [:]
    
    /// key   hashValue.description + "-" + item.rawValue
    public var callBackMap:[String:(()->())] = [:]
    
    
    
    public func addShortcutAction(item:AppShortcutItem){
        unProcessedShortcutAction[item] = 1
        NotificationCenter.default
            .post(name: .ReceivedShortcutAction, object: nil)
        callBackMap.forEach { (kv) in
            kv.value()
        }
    }
    
    open func removeListen(listener:AnyObject,item:AppShortcutItem?){
        
        guard let hashValue = listener.hashValue else {
            printE("失败...")
            return;
        }
        
        let map = callBackMap
        
        if let item = item?.rawValue {
            for (k,_) in map{
                if k == (hashValue.description + "." + item){
                    callBackMap.removeValue(forKey: k)
                }
            }
        }else{
            for (k,_) in map{
                if k.hasPrefix(hashValue.description){
                    callBackMap.removeValue(forKey: k)
                }
            }
        }
    }
    
    /// execute callback == true 执行结束
    open func listenAndTry(listener:AnyObject,item:AppShortcutItem,execute callback:@escaping ()->(Bool)){
        if let hashValue = listener.hashValue {
            let key = hashValue.description + "." + item.rawValue
            callBackMap[key] = {
                self.processedShortcutActionIfNeed(item: item, execute: callback)
            }
            callBackMap[key]?()
        }
    }
    
    /// callback == true 执行结束
    public func processedShortcutActionIfNeed(item:AppShortcutItem,execute action:(()->(Bool))){
        if unProcessedShortcutAction.keys.contains(item) {
            if action(){
                unProcessedShortcutAction.removeValue(forKey: item)
                print("此命令已销毁:\(item.rawValue)")
            }else{
                print("命令已执行:\(item.rawValue)")
            }
        }else{
            print("此命令不存在:\(item.rawValue)")
        }
    }
}

extension Notification.Name{
    public static let ReceivedShortcutAction = Notification.Name.init("ShortcutService_ReceivedShortcutAction")
}
